package com.ethereal.petmanagement.service.impl;

import com.ethereal.petmanagement.dao.impl.UserDaoImpl;
import com.ethereal.petmanagement.dto.ResponseDTO;
import com.ethereal.petmanagement.dto.UserDTO;
import com.ethereal.petmanagement.service.IUserService;
import com.ethereal.petmanagement.util.CodeMsg;
import com.ethereal.petmanagement.util.PasswordUtil;

import java.sql.Date;
import java.sql.Timestamp;

public class UserServiceImpl implements IUserService {
    static UserDaoImpl userDao = new UserDaoImpl();
    //用户登录功能
    @Override
    public ResponseDTO<UserDTO> login(UserDTO userDTO) {
        //验证码验证
        if (!userDTO.getCaptcha().equals(userDTO.getCorrectCaptcha())){
            return ResponseDTO.errorByMsg(CodeMsg.CAPTCHA_ERROR);
        }
        String inputPassword = userDTO.getPassword(); //获取登录密码
        ResponseDTO<UserDTO> responseDTO = userDao.findUserByPhone(userDTO); //dao登录
        UserDTO userDTO1 = responseDTO.getData(); //获取数据库查到的数据
        if (userDTO1.getSalt() == null){
            return ResponseDTO.errorByMsg(CodeMsg.PHONE_NOT_REGISTERED); //没有盐值，说明未注册
        }
        if (!PasswordUtil.verify(inputPassword, userDTO1.getPassword(), userDTO1.getSalt())){
            return ResponseDTO.errorByMsg(CodeMsg.PASSWORD_ERROR); //密码加密验证失败，密码错误
        }
        return ResponseDTO.success(userDTO1); //登录成功
    }

    //用户注册功能
    @Override
    public ResponseDTO<Boolean> register(UserDTO userDTO) {
        //验证手机号是否注册
        String salt = userDao.findUserByPhone(userDTO).getData().getSalt();
        if (salt != null){
            return ResponseDTO.errorByMsg(CodeMsg.PHONE_ALREADY_EXIST); //有盐值，已注册
        }
        userDTO.setId(PasswordUtil.generateId()); //设置id
        userDTO.setSalt(PasswordUtil.generateSalt()); //设置盐值
        userDTO.setCreateTime(new Timestamp(System.currentTimeMillis())); //设置创建时间
        userDTO.setPassword(PasswordUtil.encrypt(userDTO.getPassword(), userDTO.getSalt())); //设置密码加密
        //获取数据库返回的信息存入responseDTO
        if (userDao.insertUser(userDTO) == 0){
            return ResponseDTO.errorByMsg(CodeMsg.REGISTER_ERROR_DATABASE);
        }
        return ResponseDTO.success(true); //错误码为0，注册成功
    }

    //修改用户信息
    @Override
    public ResponseDTO<Boolean> updateUser(UserDTO userDTO) {
        //表单验证
        if (userDTO.getUsername().length() < 2 || userDTO.getUsername().length() > 6 || userDTO.getPhone().length() != 11){
            return ResponseDTO.errorByMsg(CodeMsg.PARAMETER_FORMAT_ERROR); //参数校验不通过
        }
        //验证手机号是否注册
        String salt = userDao.findUserByPhone(userDTO).getData().getSalt();
        if (salt != null){
            return ResponseDTO.errorByMsg(CodeMsg.PHONE_ALREADY_EXIST); //有盐值，已注册
        }
        //会话验证
        if (userDTO.getId() == null){
            return ResponseDTO.errorByMsg(CodeMsg.USER_NOT_LOGIN);
        }
        userDTO.setUpdateTime(new Timestamp(System.currentTimeMillis())); //设置更新时间
        if (userDao.updateUser(userDTO) == 0){
            return ResponseDTO.errorByMsg(CodeMsg.DATABASE_ERROR);
        }
        return ResponseDTO.success(true);
    }

    //删除用户
    public ResponseDTO<Boolean> deleteUser(UserDTO userDTO){
        if (userDTO.getId() == null){
            return ResponseDTO.errorByMsg(CodeMsg.USER_NOT_LOGIN);
        }
        int rows = userDao.deleteUser(userDTO);
        if (rows == 0){
            return ResponseDTO.errorByMsg(CodeMsg.DATABASE_ERROR);
        }
        return ResponseDTO.success(true);
    }
}
